package ren.colorful.learnjdk;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.Optional;
import java.util.logging.Logger;

import org.junit.Test;


public class CollectionsDemo {
	private static Logger LOG = Logger.getGlobal();
	
//	@Test
	public void testCollections(){
		List<String> list = Collections.EMPTY_LIST;
		List<String> list2 = Collections.<String>emptyList();
		LOG.info(list.toString());
		LOG.info(list2.toString());
	}
	
//	@Test
	public void testComapor(){
		List<Integer> list = new LinkedList<Integer>();
		list.add(3);
		list.add(1);
		list.add(2);
		list.stream().filter(i -> i > 1 ? true : false).forEach(i -> LOG.info(Objects.toString(i)));
		Collections.sort(list, new Comparator<Integer>(){
			@Override
			public int compare(Integer o1, Integer o2) {
				return o2 - o1;
			}
		});
		LOG.info(list.toString());
	}
	
//	@Test
	public void testMap(){
//		Map<String,String> map = new HashMap<String,String>();
//		map.computeIfAbsent(key, mappingFunction)
		int hash = Objects.hash("abc","efg");
		LOG.info("hash"+String.valueOf(hash));
	}
	
//	@Test
	public void testOptional(){
		Optional<String> name = Optional.of("Sanaulla");
		Optional<String> empty = Optional.ofNullable(null);
		
		if(name.isPresent()){
			LOG.info(name.get());
		}
		
		try{
			LOG.info(name.get());
		} catch (NoSuchElementException ex){
			LOG.info(ex.getMessage());
		}
		
		name.ifPresent(val -> {
			LOG.info("The length of the value is : "+ val.length());
		});
		
		LOG.info(empty.orElse("there is no value present!"));
		LOG.info(name.orElse("There is no some  vlaue!"));
		
		LOG.info(empty.orElseGet(()-> "Default value"));
		LOG.info(name.orElseGet(() -> "Default value"));
		
		try{
			empty.orElseThrow(Exception::new);
		} catch (Throwable ex){
			LOG.info(ex.getMessage());
		}
		
		String value = name.map(v -> {
			return v.toUpperCase();
		})
		.orElse("no Value found");

		LOG.info(value);
		
		String shortName = name.filter(v -> {
			return v.length() > 6;
		}).orElse("No value found");
		
		LOG.info(shortName);
		
		int i = 0;
		List<String> list = new ArrayList<String>();
		list.add("fulin");
		list.add("fulin2");
		list.forEach( v -> {
			LOG.info(v);
		});
		
	}
	
//	@Test
	public void testMapDefault(){
		Map<String,String> map = new HashMap<String,String>();
		
		map.put("fu", "fulin");
		map.put("fuq", null);
		LOG.info(map.getOrDefault("fu", "fuqiang"));
		LOG.info(map.getOrDefault("fuq", "fuqiang"));
		LOG.info(map.getOrDefault("manlij", "manli"));
	}
	
	
//	@Test
	public void testArray(){
		int[] nums = {1,4,3};
		Arrays.sort(nums);
		LOG.info(Arrays.toString(nums));
	}
	
	@Test
	public void testJubu(){
		for(int i = 0;i < getLen(10);i++){
			
		}
		
	}
	
	private int getLen(Integer len){
		LOG.info(String.valueOf(len));
		return len;
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}
